package com.yww.config;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
public class JwtConfig {
	final static String secret="yangwenwen"; //私钥
	final static long expire=3600; //失效时间


	/*
	 * 根据身份ID标识，生成Token
	 */
	public String getToken(String identityId) {
		Date nowDate = new Date();
		// 过期时间
		Date expireDate = new Date(nowDate.getTime() + expire * 1000);
		return Jwts.builder()
				.setHeaderParam("typ", "JWT")
				.setSubject(identityId)
				.setIssuedAt(nowDate)
				.setExpiration(expireDate)
				.signWith(SignatureAlgorithm.HS512, secret).compact();
	}

	/*
	 * 获取 Token 中注册信息
	 */
	public Claims getTokenClaim(String token) {
		try {
			return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/*
	 * Token 是否过期验证
	 */
	public boolean isTokenExpired(Date expirationTime) {
		return expirationTime.before(new Date());
	}

}